#include <iostream>

using namespace std;
const int N = 1e6 +10;

int l[N],r[N];

int h;
int di_gui(int x)
{
	if(!x) return 0;
	h = ( max(di_gui(l[x]) ,di_gui(r[x])) + 1 );
	return h;
}



int main()
{
	int n; cin >> n;
	for(int i = 1; i<= n;i++)
	{
		cin >> l[i] >> r[i];
	}
	cout << di_gui(1) <<endl;
	
	return 0;
}
